﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblCPerson_AttributeDAL:ItblCPerson_Attribute
    {
        HKEA_CMSEntities model;
        tblCPerson_Attribute attribute;

        public tblCPerson_AttributeDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblCPerson_AttributeDAL GetInstance()
        {
            return new tblCPerson_AttributeDAL();
        }

        public int InsertENT(tblCPerson_Attribute ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblCPerson_Attribute.AddObject(ent);
            return model.SaveChanges();
        }

        public int SaveList(List<int> AddToAttributeList, List<int> DelToAttributeList, int cpuid, string LastModifyBy)
        {
            foreach (var temp in AddToAttributeList)
            {
                var tempValue = model.tblCPerson_Attribute.Where(a => a.ContactPersonUID == cpuid).
                    Where(a => a.AttributeUID == temp).FirstOrDefault();
                if (tempValue == null)
                {
                    var ent = new tblCPerson_Attribute();
                    ent.AttributeUID = temp;
                    ent.ContactPersonUID = cpuid;
                    ent.CreationDate = DateTime.Now;
                    ent.Deleted = 0;
                    ent.LastModifyBy = LastModifyBy;
                    ent.LastModifyDate = DateTime.Now;
                    model.tblCPerson_Attribute.AddObject(ent);
                }
                else
                {
                    if (tempValue.Deleted == 1)
                    { tempValue.Deleted = 0; }
                }
            }
            foreach (var temp in DelToAttributeList)
            {
                var result = model.tblCPerson_Attribute.Where(a => a.ContactPersonUID == cpuid).
                    Where(a => a.AttributeUID == temp).
                    Where(a => a.Deleted == 0).FirstOrDefault();
                if (result != null)
                {
                    result.LastModifyDate = DateTime.Now;
                    result.LastModifyBy = LastModifyBy;
                    result.Deleted = 1;
                }
            }
            return model.SaveChanges();
        }

        public int InsertENT(List<int> attributeuidList,int cpuid,string LastModifyBy)
        {
            var toDel = model.tblCPerson_Attribute.Where(a=>a.ContactPersonUID==cpuid);
            if (toDel.Count() > 0)
            {
                foreach (var temp in toDel)
                {
                    model.tblCPerson_Attribute.DeleteObject(temp);
                }
            }                        
            foreach (var attuid in attributeuidList)
            {
                var tempENT = new tblCPerson_Attribute();
                tempENT.AttributeUID = attuid;
                tempENT.ContactPersonUID = cpuid;
                tempENT.Deleted = 0;
                tempENT.LastModifyBy = LastModifyBy;
                tempENT.CreationDate = DateTime.Now;                
                tempENT.LastModifyDate = DateTime.Now;
                model.tblCPerson_Attribute.AddObject(tempENT);
            }
            return model.SaveChanges();
        }

        public List<tblCPerson_Attribute> GetListByENT(tblCPerson_Attribute ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblCPerson_Attribute.Where(a => a.CPerson_AttributeUID > 0).Where(a=>a.Deleted==ent.Deleted);
            if (ent.CPerson_AttributeUID > 0)
                result = result.Where(a => a.CPerson_AttributeUID == ent.CPerson_AttributeUID);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a=>a.ContactPersonUID==ent.ContactPersonUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<tblCPerson_Attribute> GetListByENT(tblCPerson_Attribute ent)
        {
            var result = model.tblCPerson_Attribute.Where(a => a.CPerson_AttributeUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.CPerson_AttributeUID > 0)
                result = result.Where(a => a.CPerson_AttributeUID == ent.CPerson_AttributeUID);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a=>a.ContactPersonUID == ent.ContactPersonUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(tblCPerson_Attribute ent)
        {
            var result = model.tblCPerson_Attribute.Where(a => a.CPerson_AttributeUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.CPerson_AttributeUID > 0)
                result = result.Where(a => a.CPerson_AttributeUID == ent.CPerson_AttributeUID);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a => a.ContactPersonUID == ent.ContactPersonUID);
            if (ent.AttributeUID > 0)
                result = result.Where(a => a.AttributeUID == ent.AttributeUID);
           
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblCPerson_Attribute ent)
        {
            attribute = model.tblCPerson_Attribute.Where(a=>a.CPerson_AttributeUID==ent.CPerson_AttributeUID).FirstOrDefault();
            if (attribute != null)
            {
                attribute.Deleted = ent.Deleted;
                attribute.LastModifyBy = ent.LastModifyBy;
                attribute.LastModifyDate = DateTime.Now;
                attribute.ContactPersonUID = ent.ContactPersonUID;
                attribute.AttributeUID = ent.AttributeUID;
                attribute.ContactPersonUID = ent.ContactPersonUID;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblCPerson_Attribute ent)
        {
            var temp = model.tblCPerson_Attribute.Where(a => a.CPerson_AttributeUID > 0);
            if (ent.CPerson_AttributeUID > 0)
                temp = temp.Where(a=>a.CPerson_AttributeUID==ent.CPerson_AttributeUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblCPerson_Attribute.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }

        public bool IsExist(tblCPerson_Attribute ent)
        {
            bool ret = false;
            var result = model.tblCPerson_Attribute.Where(a => a.CPerson_AttributeUID > 0);
            if (ent.AttributeUID > 0)
                result = result.Where(a=>a.AttributeUID==ent.AttributeUID);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a=>a.ContactPersonUID==ent.ContactPersonUID);
            if (result.Count() > 0)
                ret = true;
            return ret;
        }


        public List<tblCPerson_Attribute> GetList(tblCPerson_Attribute ent, List<int> withOutList)
        {
            var result = model.tblCPerson_Attribute.Where(a=>a.CPerson_AttributeUID > 0);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a=>a.ContactPersonUID==ent.ContactPersonUID);
            if (withOutList.Count > 0)
                result = result.Where(a=>!withOutList.Contains(a.CPerson_AttributeUID));
            return result.ToList();
        }
    }
}
